/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset

* -------
* Table 3
* -------

* Globals
global dep "ln_time fraud c1_dlog_val c1_dlog_tax htrl_f_int"
global controls "tax_rate risk red_i mixed_dec differentiated valitrade_advice"


* Coefficients
* ------------

* Matrix
mat table3_1 = J(11,5,.)
mat table3_1T = J(1,5,.)

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {
	
	qui: reghdfe `x' eis_f $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)
	
	mat table3_1[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table3_1[`i',`j'] = _se[eis_f]
	mat table3_1T[1,`j'] = e(N)
	local i = `i' + 8
	mat table3_1[`i',`j'] = e(r2)
	local i = `i' -9
	local j = `j' +1
}


* Broker FE
* ---------

* New variables
tab key_dec, gen(key_dec_) // create broker id dummies

* Matrix
mat table3_2 = J(1,5,.)

* Loop for estimations
local j = 1
foreach x in $dep {
	
	* Baseline specification
	qui: reghdfe `x' eis_f $controls countryorig* max_twodigit* key_dec_* if sample_reg==1 , absorb(insp_f ym_*) cluster(insp_f)
	
	* Test joint significance of broker FE
	test key_dec_1 key_dec_2 key_dec_3 key_dec_4 key_dec_5 key_dec_6 key_dec_7 key_dec_8 key_dec_9 key_dec_10 key_dec_11 key_dec_12 key_dec_13 key_dec_14 key_dec_15 key_dec_16 key_dec_17 key_dec_18 key_dec_19 key_dec_20 key_dec_21 key_dec_22 key_dec_23 key_dec_24 key_dec_25 key_dec_26 key_dec_27 key_dec_28 key_dec_29 key_dec_30 key_dec_31 key_dec_32 key_dec_33 key_dec_34 key_dec_35 key_dec_36 key_dec_37 key_dec_38 key_dec_39 key_dec_40 key_dec_41 key_dec_42 key_dec_43 key_dec_44 key_dec_45 key_dec_46 key_dec_47 key_dec_48 key_dec_49 key_dec_50 key_dec_51 key_dec_52 key_dec_53 key_dec_54 key_dec_55 key_dec_56 key_dec_57 key_dec_58 key_dec_59 key_dec_60 key_dec_61 key_dec_62 key_dec_63 key_dec_64 key_dec_65 key_dec_66 key_dec_67 key_dec_68 key_dec_69 key_dec_70 key_dec_71 key_dec_72 key_dec_73 key_dec_74 key_dec_75 key_dec_76 key_dec_77 key_dec_78 key_dec_79 key_dec_80 key_dec_81 key_dec_82 key_dec_83 key_dec_84 key_dec_85 key_dec_86 key_dec_87 key_dec_88 key_dec_89 key_dec_90 key_dec_91 key_dec_92 key_dec_93 key_dec_94 key_dec_95 key_dec_96 key_dec_97 key_dec_98 key_dec_99 key_dec_100 key_dec_101 key_dec_102 key_dec_103 key_dec_104 key_dec_105 key_dec_106 key_dec_107 key_dec_108 key_dec_109 key_dec_110 key_dec_111 key_dec_112 key_dec_113 key_dec_114 key_dec_115 key_dec_116 key_dec_117 key_dec_118 key_dec_119 key_dec_120 key_dec_121 key_dec_122 key_dec_123 key_dec_124 key_dec_125 key_dec_126 key_dec_127 key_dec_128 key_dec_129 key_dec_130 key_dec_131 key_dec_132 key_dec_133 key_dec_134 key_dec_135 key_dec_136 key_dec_137
	
	* Scalar with the result of the p-value of the joint F test
	local p: di %9.3f r(p)
	mat table3_2[1,`j'] = `p'
	local j = `j' +1
	
}


* Export
* ------
putexcel set "$tables\Table 3.xlsx", replace sheet(Table3) // create a new excel spreadsheet
putexcel B4 = matrix(table3_1), nformat(0.000) hcenter vcenter
putexcel B12 = matrix(table3_1T), nformat(number_sep) hcenter vcenter
putexcel B14 = matrix(table3_2), nformat(0.000) hcenter vcenter

* Title
putexcel (A1:F1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Results
putexcel A2 = "Dependent variable", left vcenter
putexcel A4 = "Excess interaction share", left vcenter
putexcel A6 = "Declaration characteristics", left vcenter
putexcel A7 = "Inspector fixed effects", left vcenter
putexcel A8 = "Broker fixed effects", left vcenter
putexcel A9 = "Source country fixed effects", left vcenter
putexcel A10 = "HS2-product fixed effects", left vcenter
putexcel A11 = "Month-year fixed effects", left vcenter
putexcel A12 = "Observations", left vcenter
putexcel A13 = "R-squared", left vcenter
putexcel A14 = "P-value joint significance of broker fixed effects", left vcenter

local cells `" "B" "C" "D" "E" "F" "'
local labels `" "Time" "Fraud" "\Delta log value" "\Delta log tax" "Hyp. tax revenue losses" "'
forvalues i = 1(1)5 {
	
	local x: word `i' of `labels'
	local y: word `i' of `cells'
	
	putexcel `y'2 = "`x'", hcenter vcenter bold	
	putexcel `y'3 = "(`i')", hcenter vcenter
	
	forvalues j = 6(1)11 {
		putexcel `y'`j' = "Yes", hcenter vcenter
	}
}


* -------------------------------- ENDS HERE -------------------------------- *